#include "handy/handy/handy.h"
#include <algorithm>
#include <stdio.h>
#include <string>
#include <stdint.h>
#include <arpa/inet.h>
#include "common.h"
#include "xudp.pb.h"

using namespace std;
using namespace handy;

string g_content;
unsigned int g_id = 0;

int main() {
	g_content = random_string(500);

	setloglevel("INFO");
  setlogfile("test_client_udp");
	EventBase base;
	Signal::signal(SIGINT, [&] { base.exit(); });
	UdpConnPtr con = UdpConn::createConnection(&base, SERVER_IP, SERVER_PORT);

	exitif(!con, "start udp conn failed");

	con->onMsg([](const UdpConnPtr& p, Buffer buf) {
		info("udp recved %lu bytes", buf.size());
	});

	char buf[1000] = {};
	base.runAfter(0, [&]() {
		xmsg m;
		m.set_id(g_id++);
		m.set_buf(g_content.c_str(), MSG_SIZE);
		bool suc = m.SerializeToArray(buf, sizeof(buf));
		if (!suc) {
			fatal("serize failed");
		}
		info("id:%d,time:%ld", m.id(), util::timeMilli());
		con->send(buf, m.ByteSize()); 
		if(g_id > MAX_MSG_ID){
			base.exit();
	 	}
	}, MSG_INTERVAL);

	base.loop();
}
